package org.esisar.controller;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.esisar.controller.utils.SessionHelper;
import org.esisar.dao.AddressStatementCreator;
import org.esisar.dao.JDBCDAO;
import org.esisar.dao.UserStatementCreator;
import org.esisar.exceptions.AddressExistException;
import org.esisar.model.Address;
import org.esisar.model.User;

public class AddressController extends HttpServlet {

	public void init(ServletConfig config) throws ServletException{
		super.init(config);
	}
	
	private JDBCDAO jdbcDao = new JDBCDAO(); //TODO change this class to static
	
	public void doGet(HttpServletRequest req, HttpServletResponse res) 
			throws ServletException, IOException{

		res.setContentType("text/html");

		if (req.getParameter("address") != null) {
			createAddress(req,res);
		}
	}	
	
	private void createAddress(HttpServletRequest req, HttpServletResponse res) {
		String numero = req.getParameter("numero");
		String street = req.getParameter("street");
		String codepost = req.getParameter("codepost");
		String city = req.getParameter("city");
		String country = req.getParameter("country");
		Long  id= SessionHelper.getUserId(req);
		
		System.out.println(id);
		
		try {
			Address foundAddress = (Address) jdbcDao.find(new AddressStatementCreator(), "idClient",id+"");
			Address foundNumber = (Address) jdbcDao.find(new AddressStatementCreator() , "numero", numero);
			Address foundStreet = (Address) jdbcDao.find(new AddressStatementCreator() , "street", street);
			Address foundCodePost = (Address) jdbcDao.find(new AddressStatementCreator() , "postCode", codepost);
			Address foundCity = (Address) jdbcDao.find(new AddressStatementCreator() , "city", city);
			Address foundCountry = (Address) jdbcDao.find(new AddressStatementCreator() , "country", country);
			
			if(foundAddress != null && foundNumber != null && foundStreet != null && foundCodePost != null
					&& foundCity != null && foundCountry != null){
				throw new AddressExistException("Address already exists");
			}
			
			Address address = new Address(numero,street,codepost,city,country,id); 
			jdbcDao.addToDB(new AddressStatementCreator(), address);
			
			req.setAttribute("message", "Address created");
			try {
				req.getRequestDispatcher("User/panel.jsp").forward(req, res);
			} catch (ServletException | IOException e) {
				e.printStackTrace();
			}
		
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (AddressExistException e) {
			req.setAttribute("message", "Address already exists");
			try {
				req.getRequestDispatcher("User/panel.jsp").forward(req, res);
			} catch (ServletException | IOException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}
	}
	
}
